<template>
  <div class="warning-score">
    <basic-container>
      <avue-crud
        :option="option"
        :data="data"
        :page.sync="page"
        @on-load="onLoad"
        :search.sync="search"
        @row-save="rowSave"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-del="rowDel"
        @refresh-change="onLoad"
      >
        <template slot="menuLeft" slot-scope="{ size }"> </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { getWarnList, saveWarn, updateWarn, deleteWarn } from "@/api/scale";

export default {
  name: "WarningWarn",
  props: {
    gaugeFactorId: {
      type: String,
      default: "",
    },
    gaugeId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      user: {},
      search: {},
      promote: true,
      page: {
        pageSize: 20,
      },
      option: {
        addBtnText: "添加预警分值",
        excelBtn: false,
        delBtn: true,
        border: true,
        index: false,
        expandLevel: 3,
        headerAlign: "left",
        align: "left",
        tree: false,
        labelWidth: 100,
        menuWidth: 480,
        column: [
          {
            label: "最小分数",
            prop: "minScore",
            type: "number",
            span: 8,
            rules: [
              {
                required: true,
                message: "请输入最小分数",
                trigger: "blur",
              },
            ],
          },
          {
            label: "最大分数",
            prop: "maxScore",
            type: "number",
            span: 8,
            rules: [
              {
                required: true,
                message: "请输入最大分数",
                trigger: "blur",
              },
            ],
          },
          {
            label: "级别",
            prop: "level",
            type: "select",
            span: 8,
            dicUrl: "/api/blade-system/dict/dictionary?code=warning_level",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            dataType: "number",
            // dicData: [
            //   {
            //     label: "正常",
            //     value: 0
            //   },
            //   {
            //     label: "一级",
            //     value: 1
            //   },
            //   {
            //     label: "二级",
            //     value: 2
            //   },
            //   {
            //     label: "三级",
            //     value: 3
            //   }
            // ],
            rules: [
              {
                required: true,
                message: "请输入级别",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
    };
  },
  watch: {
    gaugeFactorId() {
      if (this.gaugeFactorId) {
        this.onLoad();
      }
    },
  },
  methods: {
    async onLoad(page = this.page, params = {}) {
      const { gaugeFactorId, gaugeId } = this;
      //模拟分页
      // this.page.total = 40;
      const {
        data: { data },
      } = await getWarnList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.search, { gaugeFactorId, gaugeId })
      );
      this.data = data.records;

      this.page.total = data.total;
    },
    goToDetailsList() {
      this.$router.push({ name: "试题详情列表" });
    },
    goToResultSet() {
      this.$router.push({ name: "结果列表" });
    },
    handleAdd() {
      this.$router.push({ name: "量表信息" });
    },

    async rowSave(form, done, loading) {
      form.gaugeId = this.gaugeId;
      form.gaugeFactorId = this.gaugeFactorId;
      await saveWarn(form);

      loading();

      this.onLoad();

      done(form);
    },
    async searchChange(params, done) {
      await this.onLoad(this.page, params);
      done();
    },
    async rowUpdate(form, index, done, loading) {
      form.gaugeId = this.gaugeId;
      form.gaugeFactorId = this.gaugeFactorId;
      await updateWarn(form);

      loading();

      done(form);
    },
    async rowDel(form) {
      await deleteWarn({ ids: form.gaugeFactorWarnId });
      this.onLoad(this.page);
    },
  },
};
</script>

<style lang="scss" scoped>
.warning-score {
}
</style>
